package cn.neusoft.controller;

import cn.neusoft.pojo.Food;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.service.IFoodService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    IFoodService foodService;

    @PostMapping      //  请求方式 + 请求路径   post  /food
    public ResultBean save(@RequestBody Food food){
        System.out.println("----FoodController-----save--------");
        System.out.println(food);
        if (food.getFoodId()==null){
            return foodService.add(food);
        }
        return foodService.update(food);
    }

//    @PutMapping    //   put   /food
//    public ResultBean update(@RequestBody Food food){
//        System.out.println("-------FoodController--------update-------------");
//        System.out.println(food);
//        return foodService.update(food);
//    }

    //  delete   /food/1003
    @DeleteMapping("/{id}")   // http://localhost:8080/food/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------FoodController--------del-------------");
        System.out.println(id);
        return foodService.delete(id);
    }

    //  get   /food/1004
    @GetMapping("/{id}")   // http://localhost:8080/food/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------FoodController--------getById-------------");
        System.out.println(id);
        return foodService.getById(id);
    }

    @GetMapping("/all")    //  get  /food/all
    public ResultBean getAll(){
        System.out.println("-------FoodController-----getAll--------");
        return foodService.getAll();
    }

    @PostMapping("/page")   // post  /food/page
    public ResultBean getByPage(@RequestBody FoodQuery foodQuery){
        System.out.println("-------FoodController--------getByPage-------------");
        System.out.println(foodQuery);
        return foodService.getByPage(foodQuery);
    }

}
